package com.liwen.web.exception;

import com.liwen.service.exception.BizException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: li.wen
 * @time: 2017-09-23 16:33
 * @description: 处理返回页面的时候出现异常的情况
 */
@ControllerAdvice(basePackages = {"com.liwen.web.controller.page"})
public class PageExceptionHandler {

    private Logger log = LoggerFactory.getLogger(this.getClass());

    @ExceptionHandler(value = Exception.class)
    public ModelAndView defaultErrorHandler(HttpServletRequest req, Exception e) {
        log.error("defaultErrorHandler-uri:{},msg:{}", req.getRequestURI(), e.getMessage());
        ModelAndView mav = new ModelAndView("error");
        mav.addObject("code", "500");
        mav.addObject("msg", "系统异常");
        return mav;
    }

    @ExceptionHandler(value = BizException.class)
    public ModelAndView bizException(HttpServletRequest req, BizException e) {
        log.error("defaultErrorHandler-uri:{},msg:{}", req.getRequestURI(), e.getMessage());
        ModelAndView mav = new ModelAndView("error");
        mav.addObject("code", e.getCode());
        mav.addObject("msg", e.getMessage());
        return mav;
    }
}
